/**
 * 
 */
package domain;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Calendar;
import java.util.GregorianCalendar;

import repositories.Noticias;

/**
 * @author David Romero Alcaide
 *
 */
public class LectorHTMLAS {
	public void leerNoticiasAS(Noticias n, String uri) {
		URL url;
		try {
			url = new URL(uri);

			InputStreamReader reader = new InputStreamReader(url.openStream());
			BufferedReader in = new BufferedReader(reader);
			String inputLine;

			do {
				inputLine = in.readLine();
				// System.out.println(inputLine);
			} while (inputLine.indexOf("<div class=\"mod_txt\">") < 0);

			while (inputLine.indexOf("<div class=\"mod_txt\">") > 0) {
				inputLine = in.readLine();
				// System.out.println(inputLine);
				Noticia not = new Noticia();
				not.setFuente("www.as.com");
				not.setUrl("www.as.com"
						+ inputLine.split("href=\"")[1].split("\"")[0]);
				not.setTitular(inputLine.split("title=\"")[1].split("\"")[0]);
				not.setDescripcion(not.getTitular());
				inputLine = in.readLine();
				not.setAutor(inputLine.split("class=\"autor\">")[1]
						.split("<span")[0]);
				System.out
						.println("iteeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee aju");
				String fecha = (inputLine.split("-")[1].split("</span>")[0]
						.trim());
				Calendar calendar = new GregorianCalendar();
				calendar.set(new Integer(fecha.substring(6, 10)), new Integer(
						fecha.substring(3, 5)),
						new Integer(fecha.substring(0, 2)));
				not.setFechaPublicacion(calendar);
				n.listaNoticias.add(not);
			}

			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
